#include "HTTPRequest.h"

//===========================================================================
//
// Default Constructor
//
HTTPRequest::HTTPRequest(void)
{
}

//===========================================================================
//
// Constructor
//
HTTPRequest::HTTPRequest(string &request,int len)
{
	_httpDataLength=len;
	int end_url_indx=request.find("\r\n");
	int end_headers_indx=request.find("\r\n\r\n");
	int start_data_indx=end_headers_indx+4;
	int length_of_headers=end_headers_indx-end_url_indx;
	int length_of_data=(request.length()-1)-start_data_indx;

	string hdrs;
	try
	{
		_url.assign(request.substr(0,end_url_indx));
		hdrs.assign(request.substr(end_url_indx+1,length_of_headers-1));
		ParseHeaders(hdrs);
		_httpData.assign(request.substr(start_data_indx,length_of_data));
	}
	catch (...)
	{
		;
	}

	setUrlExtension();
}

//===========================================================================
//
// Destructor
//
HTTPRequest::~HTTPRequest(void)
{
}


//===========================================================================
//
//	ParseHeaders() - Parse the http _headers from the _headers string
//
//				Parameters: hdrs - reference to a string containing the _headers
//
void HTTPRequest::ParseHeaders(string& hdrs)
{
	vector<string>headersLines;
	Tokenize(hdrs,headersLines,"\r\n");
	vector<string>hline;

	try
	{
		for(int i=0;i<headersLines.size();i++) // run for each header line
		{
			Tokenize(headersLines[i],hline,":");
			_headers[hline[0]]=hline[1];
			hline.clear();
		}
	}
	catch(...)
	{
		;
	}
}
//===========================================================================
//
//	Tokenize(): String tokenizer
//
void HTTPRequest::Tokenize(const string& str,vector<string>& tokens,const string& delimiters)
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}


//===========================================================================
//
//	setUrlExtension(): Retrives the _extension from the URL line
//		(GET /google/aaa/bb.html?q=11&w=22) ==> _extension="html"
//
void HTTPRequest::setUrlExtension()
{
	string suburl;
	int questionm_index;
	int leading_slash_index;
	int point_index;
	int ext_len;

	questionm_index=_url.find_first_of('?');
	if(questionm_index<0)
		return;

	leading_slash_index=_url.find_first_of('/');
	if(leading_slash_index<0)
		return;

	suburl=_url.substr(0,questionm_index);
	point_index=suburl.find_last_of('.');

	if(point_index<0)
		return;

	ext_len=suburl.length()-point_index;
	if(ext_len<2)
		return;

	_extension=suburl.substr(point_index+1,ext_len);
}
//===========================================================================
//
//	Prints http request
//
void HTTPRequest::print()
{
	cout<<_url<<endl;
	map<string,string>::const_iterator itr;
	for(itr = _headers.begin(); itr != _headers.end(); ++itr)
	{
		cout <<(*itr).first << ":" << (*itr).second<<endl;
	}
	cout<<"\n"<<_httpData<<endl;
}
//===========================================================================
//
// get methods
//
string& HTTPRequest::get_httpData()
{
	return _httpData;
}
string& HTTPRequest::get_url()
{
	return _url;
}

int HTTPRequest::get_httpDataLength()
{
	return _httpDataLength;
}

string& HTTPRequest::getUrlExtension()
{
	return _extension;
}


